/*
 * @Description:
 * @Author: wangqi
 * @Date: 2021-03-02 20:47:11
 * @LastEditTime: 2021-03-03 16:29:24
 */
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports = (env) => {
    console.log(env, "env");
    return {
        // development || production
        mode: "development",
        entry: {
            // main: { import: './src/main.js', dependOn: 'shared' },
            // index: { import: './src/index.js', dependOn: 'shared' },
            // shared: ['lodash']

            main: './src/main.js',
            index: './src/index.js',
        },
        output: {
            path: path.resolve(__dirname, './dist'),
            filename: "[name]-[chunkhash:8].js",
        },
        optimization: {
            runtimeChunk: 'single',
            splitChunks: {
                cacheGroups: {
                    vendor: {
                        test: /[\\/]node_modules[\\/]/,
                        name: 'lodash',
                        chunks: 'all',
                    },
                },
            },
        },
        devtool: 'eval-source-map',
        devServer: {
            contentBase: './dist',
            host: 'localhost',
            port: 3000,
        },
        module: {
            rules: [
                {
                    test: /\.(css)$/i,
                    use: ['style-loader', 'css-loader'],
                    include: path.resolve(__dirname, 'src'),
                },
                {
                    test: /\.(less)$/i,
                    use: ['style-loader', 'css-loader', 'less-loader'],
                },
                {
                    test: /\.(png|svg|jpg|jpeg|gif)$/i,
                    type: 'asset',
                    parser: {
                        dataUrlCondition: {
                            // <10kb转成base64(asset/inline) >10按源资源(asset/source)
                            maxSize: 10 * 1024
                        }
                    }
                },
                {
                    test: /\.(woff|woff2|eot|ttf|otf)$/i,
                    type: 'asset/resource',
                },
            ],
        },
        plugins: [
            new CleanWebpackPlugin(),
            new HtmlWebpackPlugin({
                title: 'Development',
                template: path.resolve(__dirname, './public/index.html'),
            }),
        ],

    }
}